<template>
    <div>
      <div class="bd">
        <div class="box">
            <div class="icon iconfont icon-robot-2-line"></div>
        </div>
        
        <div class="bot answer">
            <ul>
                <li>你好！我是你的 AI 美食助手。我可以帮你：</li>
                <li>• 根据食材推荐菜谱</li>
                <li>• 提供个性化的烹饪建议</li>
                <li>• 分享美食灵感和创意</li>
                <li>请告诉我你想了解什么？</li>
            </ul>
        </div>
  
        <div class="chat" v-for="item in conversation" :key="item.content">
        <div class="img" v-if="item.role === 'user' || item.role === 'assistant'">
            <!-- 修改部分 -->
            <img :src="getImageSrc(item.role)" alt="">
        </div>
        <div :class="item.role === 'user' ? 'user question' : 'bot answer'" v-if="item.role === 'user' || item.role === 'assistant'">
            <div v-html="item.content"></div>
        </div>
    </div>
  
      </div>
  
      <div class="ft">
        <div class="input">
          <input type="text" placeholder="请输入您想了解的美食信息" v-model="question"   @keyup.enter="sendIfNotEmpty">
          <button class="iconfont icon-daohang" @click="send" ></button>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue';
import axios from 'axios';
// 判断图片属性
const getImageSrc = (role) => {
    if (role === 'user') {
        return new URL('@/assets/user.png', import.meta.url).href;
    } else {
        return new URL('@/assets/robot.png', import.meta.url).href;
    }
};

//enter键会提交信息
const sendIfNotEmpty = () => {
    if (question.value.trim() !== "") {
        send();
    }
};

const conversation = reactive([]);
const question = ref("");
const send = async () => {
    if (question.value.trim() === "") {
        return;
    }
    conversation.push({
        role: "user",
        content: question.value
    });
    const msg = {
        role: "user",
        content: question.value
    };
    const res = await axios.post('http://localhost:3000/deepseek/chat', msg);
    console.log(res);
    conversation.push({
        role: "assistant",
        content: res.data.content
    });

    question.value = '';
    console.log(conversation);
};
  </script>
  
  <style lang="css" scoped>
  .hd{
    height: 60px;
    background: #FFFFFF;
    border-radius: 0px 0px 0px 0px;
    border: 1px solid #E5E7EB;
    line-height: 60px;
    padding: 0 30px;
  }
  .bd{
    height: calc(100vh - 140px);
    background: #F9FAFB;
    overflow-y: auto;
    padding: 30px 20%;
    box-sizing: border-box;
  }
  .box{
    display: flex;                
    justify-content: center;    
    align-items: center;        
    width: 32px;
    height: 32px;
    background: #FF6B6B;
    border-radius: 9999px 9999px 9999px 9999px;
    margin-right: 24px;           
  }
  .icon{
    text-align: center;
    color: #fff;
  }
  .bot{
    padding: 10px 20px;
    background: #F3F4F6;
    border-radius: 16px 16px 16px 16px;
    margin-bottom: 20px;
    line-height: 1.5;
  }
  .user{
    padding: 10px 20px;
    background: #FF6B6B;
    border-radius: 16px 16px 16px 16px;
    color: #fff;
    margin-bottom: 20px;
  }
  .ft{
    height: 80px;
    background: #FFFFFF;
    border-radius: 0px 0px 0px 0px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
    box-sizing: border-box;
  }
  .input{
    width: 700px;
    height: 100%;
    display: flex;
   
  }
  .input input{
    flex: 1;
    padding: 0 20px;
    font-size: 16px;
    background: #F3F4F6;
    border-radius: 12px 12px 12px 12px;
    border: none #FF6B6B;
    outline: none;
  }
  .input button{
    width: 80px;
    margin-left: 20px;
    background-color: #FF6B6B;
    color: #fff;
    border-radius: 4px 4px 4px 4px;
  }
  
  li{
    margin-top: 16px;
  }
  .img img {
        width: 32px; /* 设置图片宽度 */
        height: 32px; /* 设置图片高度 */
        border-radius: 50%; /* 设置图片为圆形 */
        object-fit: cover; /* 保持图片比例并覆盖容器 */
    }
    .bot.answer img {
        width: 32px; /* 设置机器人图片宽度 */
        height: 32px; /* 设置机器人图片高度 */
        border-radius: 50%; /* 设置机器人图片为圆形 */
        object-fit: cover; /* 保持图片比例并覆盖容器 */
        margin-right: 10px; /* 设置机器人图片右侧边距 */
    }
    .chat {
        display: flex;
        align-items: flex-start; /* 垂直顶部对齐 */
        margin-bottom: 20px;
    }
    .chat .img {
        margin-right: 12px; /* 头像和文本内容间隔 12px */
    }
    .chat .img img {
        width: 32px;
        height: 32px;
        border-radius: 50%;
        object-fit: cover;
    }
    .chat .user,
    .chat .bot {
        padding: 10px 20px;
        border-radius: 16px;
        line-height: 1.5;
        max-width: 70%; /* 防止文本过长 */
    }
    .chat .user {
        background: #FF6B6B;
        color: #fff;
    }
    .chat .bot {
        background: #F3F4F6;
    }
  </style>